<!DOCTYPE html>
<html>
 
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<script src="js/mui.min.js"></script>
		<link href="css/mui.min.css" rel="stylesheet" />
 
		<style type="text/css">
			.mui-grid-9 {
				background: white !important;
			}
			li {
				border-left: none !important;
			}
			img {
				width: 50px !important;
			}
			body {
				background: transparent !important;
			}
			.mui-content {
				background: white;
				text-align: center;
			}
		</style>
	</head>
 
	<body onclick="closeShare();">
		<div class="mui-content">
			<img id="pic" src="img/add.png" style="display: none;" />
		</div>
		<footer style="position: absolute;bottom: 0px; width:100%;background: white;">
			<div style="height: 30px; line-height: 30px ;font-family: '黑体'; margin-left:40%; ;margin-top: 50%;">分享本文</div>
			<ul class="mui-table-view mui-grid-view mui-grid-9">
				<li onclick="shareHref(1);" class="mui-table-view-cell mui-media mui-col-xs-4">
					<img class="mui-media-object" src="images/share_to_icon_wx.png">
					<div class="mui-media-body">微信</div>
				</li>
				<li onclick="shareHref(0);" class="mui-table-view-cell mui-media mui-col-xs-4">
					<img class="mui-media-object" src="images/share_to_icon_wxq.png">
					<div class="mui-media-body">朋友圈</div>
				</li>
				<li onclick="shareHref(2);" class="mui-table-view-cell mui-media mui-col-xs-4">
					<img class="mui-media-object" src="images/share_to_icon_qq.png">
					<div class="mui-media-body">QQ</div>
				</li>
			</ul>
		</footer>
		<input id="sharehref" style="display: none;" class="sharehref" type="url" value="www.baidu.com" placeholder="请输入要分享的链接地址" />
		<input id="sharehrefTitle" style="display: none;" class="sharehref" type="text" value="加工详情" placeholder="请输入要分享的链接标题" />
		<input id="sharehrefDes" style="display: none;" class="sharehref" type="text" value="我正在使用HBuilder+HTML5开发移动应用，赶紧跟我一起来体验！" placeholder="请输入要分享的链接描述" />
	</body>
 
	<script type="text/javascript" charset="utf-8">
      
        var shares=null;
        var Intent=null,File=null,Uri=null,main=null;
        // H5 plus事件处理
        function plusReady(){
            updateSerivces();
            if(plus.os.name=="Android"){
                main = plus.android.runtimeMainActivity();
                Intent = plus.android.importClass("android.content.Intent");
                File = plus.android.importClass("java.io.File");
                Uri = plus.android.importClass("android.net.Uri");
                main = plus.android.runtimeMainActivity();
            }
            var shareInfo = plus.webview.currentWebview().shareInfo;
			sharehref.value = shareInfo.href;
			sharehrefTitle.value = shareInfo.title;
			sharehrefDes.value = shareInfo.content;
			pageSourceId = shareInfo.pageSourceId;
			console.log("pageSource:" + pageSourceId);
        }
        if(window.plus){
            plusReady();
        }else{
            document.addEventListener("plusready",plusReady,false);
        }
        
        /**
         * 
         * 更新分享服务
         */
        function updateSerivces(){
            plus.share.getServices( function(s){
                shares={};
                for(var i in s){
                    var t=s[i];
                    shares[t.id]=t;
                }
            }, function(e){
                outSet("获取分享服务列表失败："+e.message );
            } );
        }
        
        /**
           * 分享操作
           * @param {JSON} sb 分享操作对象s.s为分享通道对象(plus.share.ShareService)
           * @param {Boolean} bh 是否分享链接
           */
        function shareAction(sb,bh) {
            if(!sb||!sb.s){
                console.log("无效的分享服务！");
                return;
            }
            if (plus.os.name !== "Android") {
					plus.nativeUI.alert("此平台暂不支持系统分享功能!");
					return;
			}
            
            var msg={content:sharehrefDes.value,extra:{scene:sb.x}};
            if(bh){
                msg.href=sharehref.value;
                if(sharehrefTitle&&sharehrefTitle.value!=""){
                    msg.title=sharehrefTitle.value;
                }
                if(sharehrefDes&&sharehrefDes.value!=""){
                    msg.content=sharehrefDes.value;
                }
                msg.thumbs=["_www/images/logo1024.png"];
                msg.pictures=["_www/images/logo1024logo.png"];
            }else{
                if(pic&&pic.realUrl){
                    msg.pictures=[pic.realUrl];
                }
            }
            // 发送分享
            if ( sb.s.authenticated ) {
                console.log("---已授权---");
                shareMessage(msg,sb.s);
            } else {
                console.log("---未授权---");
                sb.s.authorize( function(){
                        shareMessage(msg,sb.s);
                    },function(e){
                        console.log("认证授权失败："+e.code+" - "+e.message );
                    
                });
            }
        }
        /**
           * 发送分享消息
           * @param {JSON} msg
           * @param {plus.share.ShareService} s
           */
        function shareMessage(msg,s){
            
            console.log(JSON.stringify(msg));
            s.send( msg, function(){
                console.log("分享到\""+s.description+"\"成功！ " );
                
            }, function(e){
                console.log( "分享到\""+s.description+"\"失败: "+JSON.stringify(e) );
            
            } );
        }
        // 分析链接
        function shareHref(index){
            var shareBts=[];
            // 更新分享列表
            var ss=shares['weixin'];
            ss&&ss.nativeClient&&(shareBts.push({title:'微信朋友圈',s:ss,x:'WXSceneTimeline'}),
            shareBts.push({title:'微信好友',s:ss,x:'WXSceneSession'}));
            ss=shares['qq'];
            ss&&ss.nativeClient&&shareBts.push({title:'QQ',s:ss});
         
          	 shareAction(shareBts[index],true);
        }
        mui.back = function() {
			var sourcePage = plus.webview.getWebviewById(pageSourceId);
			if (sourcePage) {
				sourcePage.evalJS("closeMask()");
			}
		}
 
		function closeShare() {
			console.log("e:" + event.target.name);
		}
        
        </script>
 
</html>
